/*
 * Copyright (c) 2021 Zsj. All rights reserved.
 */

package com.jzz.crawlerweb.handler.generator.impl;

import com.jzz.crawlerweb.handler.generator.ImageHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;

public class NoiseImageHandler implements ImageHandler {
    private static final Logger logger = LoggerFactory.getLogger(NoiseImageHandler.class);

    private final Random random = new Random();

    @Override
    public BufferedImage handle(BufferedImage bi, Object... args) {
        int count = 3;
        BufferedImage noise = new BufferedImage(bi.getWidth(), bi.getHeight(), BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = noise.createGraphics();
        g2d.setColor(new Color(0, 0, 0, 101));
        g2d.setStroke(new BasicStroke(4));

        for (int i = 0; i < count; i++) {
            // line
            int len = 100 + random.nextInt(10);
            int x = random.nextInt(bi.getWidth());
            int y = random.nextInt(bi.getHeight());
            double angle = (double) random.nextInt(360) / Math.PI;
            int endX = (int) (Math.cos(angle) * len);
            int endY = (int) (Math.sin(angle) * len);
            g2d.drawLine(x, y, endX, endY);
        }

        Graphics2D biG2d = bi.createGraphics();
        biG2d.drawImage(noise, 0, 0, null);
        g2d.dispose();
        return bi;
    }
}
